﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace WebDataGet.Utils
{
    public class MyXmlConfigHelper<T>
    {
        public T Setting { get; set; }

        public string FileName { get; set; } = "MyConfig.xml";

        public string DirectoryPath
        {
            get
            {
                var regex = new Regex(@"\\(\w+)\.(\w+)$");
                return regex.Split(FullPath)[0];
            }
        }
        public string DebugPath { get => Directory.GetCurrentDirectory(); }

        public string FullPath { get => DebugPath + "\\" + FileName; }

        public bool IsFileExist { get => File.Exists(FullPath); }

        public bool IsDirectoryExist { get => Directory.Exists(DirectoryPath); }

        public Action<string> ShowMsg { get; set; } = (msg) => Console.WriteLine(msg);

        public MyXmlConfigHelper()
        {

        }
        public MyXmlConfigHelper(string filename)
        {
            FileName = filename;
            if (!IsDirectoryExist)
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(DirectoryPath);
                directoryInfo.Create();
            }
        }

        public MyXmlConfigHelper(T setting, string filename) : this(filename)
        {
            Setting = setting;
        }

        /// <summary>
        /// 创建文件
        /// </summary>
        public void Init()
        {
            if (IsFileExist)
            {
                try
                {
                    Read();
                }
                catch (Exception ex)
                {
                    ShowMsg(ex.ToString());
                    throw new Exception("文件读取失败!请确认是否配置文件格式是否正确");
                }
            }
            else
            {
                Write();
            }
        }

        /// <summary>
        /// 覆盖文件
        /// </summary>
        public void ReInit()
        {
            ShowMsg("正在覆盖配置文件：" + FullPath);
            Write();
        }
        /// <summary>
        /// 写入配置类
        /// </summary>
        public void Write()
        {
            ShowMsg("正在生成配置文件：" + FullPath);
            var xmlHelper = new XmlSerializer(typeof(T));
            using (StreamWriter xmlWriter = new StreamWriter(FullPath))
            {
                //去掉烦人的命名空间
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");
                xmlHelper.Serialize(xmlWriter, Setting, ns);
                xmlWriter.Close();
            }
        }

        /// <summary>
        /// 读取配置类
        /// </summary>
        public void Read()
        {
            ShowMsg("正在读取配置文件：" + FullPath);
            var xmlHelper = new XmlSerializer(typeof(T));
            using (StreamReader xmlReader = new StreamReader(FullPath))
            {

                Setting = (T)xmlHelper.Deserialize(xmlReader);
                xmlReader.Close();
            }
        }
    }
}
